//
//  TRNewsGoodsViewController.m
//  WeiMiLan
//
//  Created by Mac on 14-9-19.
//  Copyright (c) 2014年 Mac. All rights reserved.
//
#import "TRsearchViewController.h"
#import "TRMemuCell.h"
#import "TRNewsGoodsViewController.h"
#import "TRHeaderView.h"

@interface TRNewsGoodsViewController ()

@property(nonatomic,strong)NSMutableArray * searchArray;
@property(nonatomic,strong)NSMutableArray * idArray;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic,assign)int  pageNum;
@property(nonatomic,assign)NSInteger oldNum;


@property(nonatomic,strong)NSArray * pageDict;


@property (strong,nonatomic)NSMutableArray *dataSouth;

@property (strong,nonatomic)NSString * timeString;

@property (strong,nonatomic)NSArray * headMenus;
//时间差
@property (nonatomic)int i;
@property (strong,nonatomic)NSArray *menus;
@end

@implementation TRNewsGoodsViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
#pragma mark-----退出
-(void)gotoMainPage{
    [self.navigationController popViewControllerAnimated:YES];
   // [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)initNavigation
{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.weChatNavigationBar=[[WeChatNavigationBar alloc] init];
    [self.view addSubview:self.weChatNavigationBar];
    //    self.weChatNavigationBar.titleLabel.text=self.album[0][@"NAME"];
     
    self.weChatNavigationBar.titleLabel.text=@"最新情报";
    [self.weChatNavigationBar .leftButton addTarget:self action:@selector(gotoMainPage)  forControlEvents:UIControlEventTouchUpInside];
   self.weChatNavigationBar .rightButton.hidden = YES;
//    [self.weChatNavigationBar.rightButton setImage:[UIImage imageNamed:@"分类"] forState:0];
//    [self.weChatNavigationBar.rightButton addTarget:self action:@selector(showMenuView) forControlEvents:UIControlEventTouchUpInside];
//    self.weChatNavigationBar.rightButton.height=35;
//    self.weChatNavigationBar.rightButton.width=35;
//    self.weChatNavigationBar.rightButton.frame=CGRectMake(266,22,35,35);
//    
//    [self.weChatNavigationBar.rightButton setImageEdgeInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenMenuView)];
    [self.weChatNavigationBar addGestureRecognizer:tap];
    
    //    self.weChatNavigationBar rightButton
    
}

#pragma mark----隐藏
-(void)hiddenMenuView
{

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.menus=@[@"服装",@"包包",@"丝巾",@"鞋子",@"腕表",@"皮带",@"钱夹",@"饰品",@"眼镜"];
    self.headMenus=@[@"服装1",@"包包1",@"丝巾1",@"鞋子1",@"腕表1",@"皮带1",@"钱夹1",@"饰品1",@"眼镜1"];
    self.i=0;
    
    self.pageNum=1;
    
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"TRMemuCell" bundle:nil] forCellWithReuseIdentifier:@"TRMemuCell"];
    
    CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
    
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.headerHeight = 0;
    layout.footerHeight = 0;
//    layout.minimumColumnSpacing = 10;
//    layout.minimumInteritemSpacing = 10;
    layout.columnCount = 1;
    self.collectionView.alwaysBounceVertical = YES;
    [self.collectionView setCollectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self initNavigation];
    
    self.timeLabel.text=[CurrentDate gggStringWith:_i];
    
    NSString *s=[CurrentDate getCurrentDateString];
    
    [self initData:s];
    
}

-(void)showMenuView
{
    TRTabBarViewController *tvc=(TRTabBarViewController *)self.tabBarController;
    tvc.delegate=self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.pageNum=1;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.collectionView.contentOffset=CGPointMake(0, 0);
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}



- (void)menuViewDidSelecte:(NSInteger) index
{
//   NSLog(@"-----%@",self.menus[index]);
    @try {
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:index] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
    }
    @catch (NSException *exception) {
        
    //    NSLog(@"跳转失败");
        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"亲，暂时没有新的%@上传啊",self.menus[index]]];
    }
    @finally {
        
    }
}

-(void)initData:(NSString * )timeString{
    NSString * requestString=[NSString stringWithFormat:@"http://ping.vsmu.cn/app/Interface/Package?type=1021&BEGIN_DATE=%@000000&END_DATE=%@232323&BID=%@&userId=%@",timeString,timeString,ApplicationDelegate.markString,ApplicationDelegate.currentUserId];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: requestString]];
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [QLMaskUtil hideLoadingMaskWithView:self.collectionView];
        NSString *requestTmp = [NSString stringWithString:operation.responseString];
        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        //系统自带JSON解析
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
        
        NSLog(@"搜索信息:%@",[NSString replaceUnicode:resultDic.description]);
        self.pageDict = resultDic[@"PACKAGE_RSP"][@"PACKAGE_TYPE"];
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [QLMaskUtil hideLoadingMaskWithView:self.collectionView];
        NSLog(@"Failure: %@",error);
        
        MMProgressHUDShowError(@"提交失败，请重试");
        
    }];
    [operation start];
}

#pragma mark - collectview部分
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSDictionary *d=self.pageDict[section];
    
    NSArray * searchArray=d[@"PACKAGE"];
    
    return searchArray.count;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    
    return self.pageDict.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TRMemuCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"TRMemuCell" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor grayColor];
    NSDictionary *d=self.pageDict[indexPath.section];
    NSArray * searchArray=d[@"PACKAGE"];
    cell.info = [searchArray objectAtIndex:indexPath.row];
  //  cell.selectImageView.hidden= YES;
    return cell;
}



- ( UICollectionReusableView * ) collectionView : ( UICollectionView * ) collectionView viewForSupplementaryElementOfKind : ( NSString * ) kind atIndexPath : ( NSIndexPath * ) indexPath
{
    UICollectionReusableView * reusableview = nil ;
    
    
    TRHeaderView * headerView = [ collectionView dequeueReusableSupplementaryViewOfKind : UICollectionElementKindSectionHeader withReuseIdentifier : @ "HeaderView" forIndexPath : indexPath ] ;
    NSDictionary *d=self.pageDict[indexPath.section];
    
    //NSString * title =
    
    NSString *imgString=self.headMenus[indexPath.section];
    headerView.headerImg.image=[UIImage imageNamed:imgString];
    
    headerView.title.text=[NSString stringWithFormat:@"%@>",d[@"NAME"]];
    reusableview = headerView;
    
    
    return reusableview;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kUI_SCREEN_WIDTH, 20);
}

-(void)setI:(int)i
{
    _i=i;
    self.timeLabel.text=[CurrentDate gggStringWith:i];
    [QLMaskUtil showMaskLoadingWithView:self.collectionView];
    [self initData:[CurrentDate getDifferenceDateStringWith:self.i]];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TRTabBarViewController *tvc=(TRTabBarViewController *)self.tabBarController;
    
    NSDictionary *d=self.pageDict[indexPath.section];
    NSArray * searchArray=d[@"PACKAGE"];
    
    TRProductDetailViewController *vc = [[TRProductDetailViewController alloc] init];
    vc.productTypeId = searchArray[indexPath.row][@"ID"];
    [self presentViewController:vc animated:YES completion:nil];
}

- (IBAction)changTime:(UIButton *)sender
{
    switch (sender.tag) {
        case 1:
            self.i=self.i-1;
            
            break;
        case 2:
            
            self.i=self.i+1;
            
            break;
            
        default:
            break;
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *d=self.pageDict[indexPath.section];
    NSArray * searchArray=d[@"PACKAGE"];
    TRMemuCell *cell =[[TRMemuCell alloc] init];
    cell.info = [searchArray objectAtIndex:indexPath.row];
    return [cell getSize];
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}


@end
